package cn.neusoft.dao.impl;

import cn.neusoft.bean.Business;
import cn.neusoft.bean.Cart;
import cn.neusoft.dao.ICartDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.BusinessQuery;
import cn.neusoft.query.CartQuery;
import cn.neusoft.utils.JdbcUtils;
import com.alibaba.druid.util.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDao implements ICartDao {

    private QueryRunner queryRunner;

    public CartDao(){
        queryRunner = JdbcUtils.getQueryRunner();
    }

    @Override
    public int add(Cart cart) throws SQLException {
        String sql = "INSERT INTO cart(foodId,businessId,userId,quantity) VALUES(?,?,?,?)";
        return queryRunner.update(sql,
                cart.getFoodId(),
                cart.getBusinessId(),
                cart.getUserId(),
                cart.getQuantity());
    }

    @Override
    public int update(Cart cart) throws SQLException {
        String sql = "UPDATE cart SET foodId=?,businessId=?,userId=?,quantity=? WHERE cartId=?";
        return queryRunner.update(sql,
                cart.getFoodId(),
                cart.getBusinessId(),
                cart.getUserId(),
                cart.getQuantity(),
                cart.getCartId());
    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from cart where cartId=?";
        return queryRunner.update(sql,id);
    }

    @Override
    public Cart getById(Integer id) throws SQLException {
        String sql = "SELECT cartId,foodId,businessId,userId,quantity FROM cart WHERE cartId=?";
        return queryRunner.query(sql, new BeanHandler<Cart>(Cart.class), id);
    }

    @Override
    public List<Cart> getAll() throws SQLException {
        String sql = "SELECT cartId,foodId,businessId,userId,quantity FROM cart";
        return queryRunner.query(sql, new BeanListHandler<Cart>(Cart.class));
    }

    @Override
    public int getCount(BaseQuery baseQuery) throws SQLException {
        CartQuery query = (CartQuery)baseQuery;
        List<Object> params = new ArrayList<>();
        String sql = "select count(cartId) from cart where 1=1 ";
        if(!StringUtils.isEmpty(query.getUserId())) {
            sql += " and userId like concat('%',?,'%')";
            params.add(query.getUserId());
        }
        if (query.getBusinessId()!=null && query.getBusinessId()!=0){
            sql += " and businessId like concat('%',?,'%')";
            params.add(query.getBusinessId());
        }
        System.out.println("查询总记录："+sql);
        System.out.println("参数集合："+params);

        return queryRunner.query(sql, new ScalarHandler<Long>(), params.toArray()).intValue();
    }

    @Override
    public List<Cart> getByPage(BaseQuery baseQuery) throws SQLException {
        CartQuery query = (CartQuery)baseQuery;
        List<Object> params = new ArrayList<>();
        String sql = "SELECT cartId,foodId,businessId,userId,quantity FROM cart" +
                " where 1=1";
        if(!StringUtils.isEmpty(query.getUserId())) {
            sql += " and userId like concat('%',?,'%')";
            params.add(query.getUserId());
        }
        if (query.getBusinessId()!=null && query.getBusinessId()!=0){
            sql += " and businessId like concat('%',?,'%')";
            params.add(query.getBusinessId());
        }
        sql += " ORDER BY cartId DESC limit ?,?";
        params.add(query.getStartIndex());
        params.add(query.getPageCount());

        System.out.println("查询集合："+sql);
        System.out.println("参数集合："+params);

        return queryRunner.query(sql, new BeanListHandler<Cart>(Cart.class), params.toArray());
    }
}
